home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 569 / c_libs1a / alert.c next >
C/C++ Source or Header  |  1992-04-30  |  11KB  |  436 lines

  1. /*********************************************/
  2. /*   Generic substution form_alert() code &  */
  3. /*            form_center() code.            */ 
  4. /*           Written in Lattice C 5          */
  5. /*              by Scott Sanders             */
  6. /*    (c)1991 Software Development Systems   */
  7. /*        Started 12/31/91, 02:39 AM         */
  8. /*  Version 1.0: Finished 1/01/92, 05:43 AM  */
  9. /*  Version 1.1: Finished 2/24/92, 02:19 AM  */
  10. /*********************************************/
  11.  
  12. /* Includes */
  13.  
  14. #include <AES.H>
  15. #include <DOS.H>
  16. #include <VDI.H>
  17. #include <STRING.H>
  18. #include <STDIO.H>
  19. #include "ALERT.H"
  20.  
  21. /* Resource for Alert */
  22.  
  23. static short IMAG0[] = {
  24.     0x0003,0xc000,0x0006,0x6000,
  25.     0x000d,0xb000,0x001b,0xd800,
  26.     0x0037,0xec00,0x006f,0xf600,
  27.     0x00dc,0x3b00,0x01bc,0x3d80,
  28.     0x037c,0x3ec0,0x06fc,0x3f60,
  29.     0x0dfc,0x3fb0,0x1bfc,0x3fd8,
  30.     0x37fc,0x3fec,0x6ffc,0x3ff6,
  31.     0xdffc,0x3ffb,0xbffc,0x3ffd,
  32.     0xbffc,0x3ffd,0xdffc,0x3ffb,
  33.     0x6ffc,0x3ff6,0x37fc,0x3fec,
  34.     0x1bff,0xffd8,0x0dff,0xffb0,
  35.     0x06fc,0x3f60,0x037c,0x3ec0,
  36.     0x01bc,0x3d80,0x00dc,0x3b00,
  37.     0x006f,0xf600,0x0037,0xec00,
  38.     0x001b,0xd800,0x000d,0xb000,
  39.     0x0006,0x6000,0x0003,0xc000
  40. };
  41.  
  42. static short IMAG1[] = {
  43.     0x3fff,0xfffc,0xc000,0x0003,
  44.     0x9fff,0xfff9,0xbfff,0xfffd,
  45.     0xdff8,0x3ffb,0x5fe0,0x0ffa,
  46.     0x6fc0,0x07f6,0x2f83,0x83f4,
  47.     0x3787,0xc3ec,0x1787,0xc3e8,
  48.     0x1bff,0x83d8,0x0bff,0x07d0,
  49.     0x0dfe,0x0fb0,0x05fc,0x1fa0,
  50.     0x06fc,0x3f60,0x02fc,0x3f40,
  51.     0x037c,0x3ec0,0x017c,0x3e80,
  52.     0x01bf,0xfd80,0x00bf,0xfd00,
  53.     0x00dc,0x3b00,0x005c,0x3a00,
  54.     0x006c,0x3600,0x002f,0xf400,
  55.     0x0037,0xec00,0x0017,0xe800,
  56.     0x001b,0xd800,0x000b,0xd000,
  57.     0x000d,0xb000,0x0005,0xa000,
  58.     0x0006,0x6000,0x0003,0xc000
  59. };
  60.  
  61. static short IMAG2[] = {
  62.     0x007f,0xfe00,0x00c0,0x0300,
  63.     0x01bf,0xfd80,0x037f,0xfec0,
  64.     0x06ff,0xff60,0x0dff,0xffb0,
  65.     0x1bff,0xffd8,0x37ff,0xffec,
  66.     0x6fff,0xfff6,0xdfff,0xfffb,
  67.     0xb181,0x860d,0xa081,0x0205,
  68.     0xa4e7,0x3265,0xa7e7,0x3265,
  69.     0xa3e7,0x3265,0xb1e7,0x3205,
  70.     0xb8e7,0x320d,0xbce7,0x327d,
  71.     0xa4e7,0x327d,0xa0e7,0x027d,
  72.     0xb1e7,0x867d,0xbfff,0xfffd,
  73.     0xdfff,0xfffb,0x6fff,0xfff6,
  74.     0x37ff,0xffec,0x1bff,0xffd8,
  75.     0x0dff,0xffb0,0x06ff,0xff60,
  76.     0x037f,0xfec0,0x01bf,0xfd80,
  77.     0x00c0,0x0300,0x007f,0xfe00
  78. };
  79.  
  80. static BITBLK rs_bitblk[] = {
  81.     IMAG0,4,32,0,0,0x1,
  82. };
  83.  
  84. static OBJECT rs_object[] = {
  85.     -1,1,9,G_BOX,NORMAL,OUTLINED,(void *) 0x21100L,0x5,0x2,0x28,0x9,
  86.     2,-1,-1,G_IMAGE,NORMAL,NORMAL,(void *) 0L,0x2,0x1,0x4,0x2,
  87.     3,-1,-1,G_STRING,NORMAL,NORMAL,(void *) 0L,0x8,0x1,0x1e,0x1,
  88.     4,-1,-1,G_STRING,NORMAL,NORMAL,(void *) 1L,0x8,0x2,0x1e,0x1,
  89.     5,-1,-1,G_STRING,NORMAL,NORMAL,(void *) 2L,0x8,0x3,0x1e,0x1,
  90.     6,-1,-1,G_STRING,NORMAL,NORMAL,(void *) 3L,0x8,0x4,0x1e,0x1,
  91.     7,-1,-1,G_STRING,NORMAL,NORMAL,(void *) 4L,0x8,0x5,0x1e,0x1,
  92.     8,-1,-1,G_BUTTON,SELECTABLE|EXIT,NORMAL,(void *) 5L,0x1,0x7,0xc,0x1,
  93.     9,-1,-1,G_BUTTON,SELECTABLE|EXIT,NORMAL,(void *) 6L,0xe,0x7,0xc,0x1,
  94.     0,-1,-1,G_BUTTON,SELECTABLE|EXIT|LASTOB,NORMAL,(void *) 7L,0x1b,0x7,0xc,0x1
  95. };
  96.  
  97. #define ANUM_OBS      10
  98.  
  99. /* Module Variables */
  100.  
  101. static short rsc_fix,cwidth,cheight,handle,planes,scrwid,scrhigh;
  102. static OBJECT *tree;
  103.  
  104. /* Prototypes */
  105.  
  106. int form_nalert();
  107. static void ObjcExtent(),init();
  108. void form_ncenter();
  109.  
  110. int
  111. form_nalert(rect,def,work,image)
  112. GRECT *rect;        /* Rectangle to center over: NULL = old way      */
  113. short def;            /* Default button: 0 = None                    */
  114. char *work;            /* Standard GEM Alert String                    */
  115. short *image;        /* Custom Image: 32x32 icon (64 shorts) or NULL    */
  116. {
  117.     char lines[5][31],buttons[3][11],*cp;
  118.     short inc,type,num_lines=0,num_buttons=0,btmp,ltmp,tmp,j,ret,bwid=0,pixwid;
  119.     GRECT area;
  120.     long reserve,asize,rhigh,rlow,shigh,slow;
  121.     char *holder;
  122.     short pxyarray[8],hint;
  123.     MFDB src,dest;
  124.  
  125.     if(!rsc_fix)
  126.         init();
  127.     
  128.     ObSpec(ICON) = (BITBLK *) &rs_bitblk[0];
  129.     ObSpec(LINE1) = lines[0];
  130.     ObSpec(LINE2) = lines[1];
  131.     ObSpec(LINE3) = lines[2];
  132.     ObSpec(LINE4) = lines[3];
  133.     ObSpec(LINE5) = lines[4];
  134.     ObSpec(BUTTON1) = buttons[0];
  135.     ObSpec(BUTTON2) = buttons[1];
  136.     ObSpec(BUTTON3) = buttons[2];
  137.     
  138.     for(inc=0;inc<6;inc++)    /* Clear Arrays */
  139.         lines[inc][0] = 0;
  140.     for(inc=0;inc<3;inc++)
  141.         buttons[inc][0] = 0;
  142.     
  143.     cp = work;            /* Parse alert string into arrays. */
  144.     if(*cp++!='[')
  145.         return NAFORMERR;    /* Function returns 0 if error occurs */
  146.     if(*cp<'0' || *cp>'4')
  147.         return NAFORMERR;
  148.     type = *cp++ - '0';
  149.     if(*cp++!=']')
  150.         return NAFORMERR;
  151.     if(*cp++!='[')
  152.         return NAFORMERR;
  153.     while(*cp!='[') {
  154.         inc = 0;
  155.         while(*cp!='|' && *cp!=']') {
  156.             lines[num_lines][inc] = *cp++;
  157.             if(inc==30)
  158.                 return NAFORMERR;
  159.             if(*cp==0)
  160.                 return NAFORMERR;
  161.             inc++;
  162.         }
  163.         lines[num_lines][inc] = 0;
  164.         *cp++;
  165.         num_lines++;
  166.         if(num_lines>5)
  167.             num_lines = 5;
  168.     }
  169.     if(*cp++!='[')
  170.         return NAFORMERR;
  171.     while(*cp!=0) {
  172.         inc = 0;
  173.         while(*cp!='|' && *cp!=']') {
  174.             buttons[num_buttons][inc] = *cp++;
  175.             if(inc==10)
  176.                 return NAFORMERR;
  177.             if(*cp==0)
  178.                 return NAFORMERR;
  179.             inc++;
  180.         }
  181.         buttons[num_buttons][inc] = 0;
  182.         num_buttons++;
  183.         *cp++;
  184.         if(num_buttons>3)
  185.             num_buttons = 3;
  186.     }
  187.     /* Select an Icon */
  188.     ShowObj(ICON);
  189.     switch(type) {
  190.         case 0:
  191.             BiData(0) = IMAG0;    /* Still Needs to Point Somewhere */
  192.             HideObj(ICON);
  193.             break;
  194.         case 1:
  195.             BiData(0) = IMAG0;
  196.             break;
  197.         case 2:
  198.             BiData(0) = IMAG1;
  199.             break;
  200.         case 3:
  201.             BiData(0) = IMAG2;
  202.             break;
  203.         case 4:
  204.             BiData(0) = image;
  205.             break;
  206.     }
  207.  
  208.     for(inc=LINE1;inc<=LINE5;inc++) {    /* Adjust Alert Lines */
  209.         if(type==0)
  210.             ObX(inc) = cwidth*2;
  211.         else
  212.             ObX(inc) = cwidth*4+32;
  213.         if(num_lines>inc-LINE1) {
  214.             ObW(inc) = (int)(strlen(lines[inc-LINE1])*cwidth);
  215.             ShowObj(inc);
  216.         }
  217.         else
  218.             HideObj(inc);
  219.     }
  220.  
  221.     for(inc=BUTTON1;inc<=BUTTON3;inc++) {    /* Adjust Buttons */
  222.         if(num_buttons>inc-BUTTON1) {
  223.             j = (int)((strlen(buttons[inc-BUTTON1])+1)*cwidth);
  224.             if(j>bwid)
  225.                 bwid = j;
  226.             ShowObj(inc);
  227.         }
  228.         else
  229.             HideObj(inc);
  230.     }
  231.     for(inc=BUTTON1;inc<BUTTON1+num_buttons;inc++) {
  232.         ObW(inc) = bwid+cwidth*1;
  233.         if(inc>BUTTON1)
  234.             ObX(inc) = ObX(inc-1)+ObW(inc-1)+(cwidth*2);
  235.         else
  236.             ObX(inc) = cwidth*2;
  237.     }
  238.     
  239.     /* Find out minimum alert width */
  240.     
  241.     btmp = (cwidth*2)+ObX(num_buttons-1+BUTTON1)+ObW(num_buttons-1+BUTTON1);
  242.     ltmp = 0;
  243.     for(inc=LINE1;inc<LINE1+num_lines;inc++) {
  244.         if(ObW(inc)>ltmp)
  245.             ltmp = ObW(inc);
  246.     }
  247.     ltmp += (cwidth*4)+(type==0 ? 0 : 32+(cwidth*2));
  248.     if(ltmp>btmp) {
  249.         tmp = (ltmp-(ObX(num_buttons-1+BUTTON1)+ObW(num_buttons-1+BUTTON1)-ObX(BUTTON1)))/2;
  250.         j = tmp-ObX(BUTTON1);
  251.         for(inc=BUTTON1;inc<BUTTON1+num_buttons;inc++)
  252.             ObX(inc) += j;
  253.         ObW(ROOT) = ltmp;
  254.     }
  255.     else
  256.         ObW(ROOT) = btmp;
  257.  
  258.     /* Find out minimum alert height */
  259.     ltmp = cheight*(num_lines+4);
  260.     btmp = (cheight*4)+32;
  261.     ObH(ROOT) = max(ltmp,btmp);
  262.     for(inc=BUTTON1;inc<=BUTTON3;inc++) {
  263.         ObY(inc) = ObH(ROOT)-cheight*2;
  264.         if(inc-BUTTON1+1 == def)
  265.             MakeDefault(inc);
  266.         else
  267.             NoDefault(inc);
  268.     }
  269.  
  270.     /* Center Object */
  271.     if(rect==NULL) {
  272.         area.g_x = 0;
  273.         area.g_y = 0;
  274.         area.g_w = scrwid;
  275.         area.g_h = scrhigh;
  276.     }
  277.     else {
  278.         area.g_x = rect->g_x;
  279.         area.g_y = rect->g_y;
  280.         area.g_w = rect->g_w;
  281.         area.g_h = rect->g_h;
  282.     }
  283.     form_ncenter(tree,&area);
  284.     
  285.     /* Enable Clipping */
  286.     pxyarray[0] = 0;
  287.     pxyarray[1] = 0;
  288.     pxyarray[2] = scrwid;
  289.     pxyarray[3] = scrhigh;
  290.     vs_clip(handle,1,pxyarray);
  291.  
  292.      pixwid = area.g_w;
  293.      if(pixwid%16 != 0)
  294.          pixwid +=16-(pixwid%16); 
  295.      hint = (pixwid/16*planes);
  296.  
  297.     reserve = (long)(hint*2*area.g_h);
  298.  
  299.     wind_get(DESK,WF_SCREEN,(short *)&rhigh,(short *)&rlow,(short *)&shigh,(short *)&slow);
  300.     holder = (char *)(rhigh | ((rlow>>16)&0xFFFF));
  301.     asize = (shigh | ((slow>>16)&0xFFFF));
  302.  
  303.     if(reserve>asize) {
  304.         vs_clip(handle,0,NULL);
  305.         return NAMEMERR;
  306.     }
  307.  
  308.       pxyarray[0] = area.g_x;
  309.       pxyarray[1] = area.g_y;
  310.       pxyarray[2] = area.g_x+area.g_w;
  311.       pxyarray[3] = area.g_y+area.g_h;
  312.       pxyarray[4] = 0;
  313.       pxyarray[5] = 0;
  314.       pxyarray[6] = area.g_w;
  315.       pxyarray[7] = area.g_h;
  316.       src.fd_addr = NULL;
  317.       dest.fd_addr = holder;
  318.       dest.fd_w = pixwid;
  319.       dest.fd_h = (short)(reserve/(pixwid/8));
  320.       dest.fd_wdwidth = pixwid/16; 
  321.       dest.fd_stand = 0;
  322.       dest.fd_nplanes = planes;
  323.  
  324.       graf_mouse(M_OFF,NULL);
  325.       vro_cpyfm(handle,S_ONLY,pxyarray,&src,&dest);
  326.     graf_mouse(M_ON,NULL);
  327.  
  328.     objc_draw(tree,ROOT,MAX_DEPTH,ELTS(area));
  329.     ret = form_do(tree,0);
  330.     Deselect(ret);
  331.  
  332.     dest.fd_addr = NULL;
  333.     src.fd_addr = holder;
  334.     src.fd_w = pixwid;
  335.     src.fd_h = (short)(reserve/(pixwid/8));
  336.     src.fd_wdwidth = pixwid/16;
  337.     src.fd_stand = 0;
  338.     src.fd_nplanes = planes;
  339.     pxyarray[0] = 0;
  340.     pxyarray[1] = 0;
  341.     pxyarray[2] = area.g_w;
  342.     pxyarray[3] = area.g_h;
  343.     pxyarray[4] = area.g_x;
  344.     pxyarray[5] = area.g_y;
  345.     pxyarray[6] = area.g_x+area.g_w;
  346.     pxyarray[7] = area.g_y+area.g_h;
  347.  
  348.     graf_mouse(M_OFF,NULL);
  349.     vro_cpyfm(handle,S_ONLY,pxyarray,&src,&dest);
  350.      graf_mouse(M_ON,NULL);
  351.  
  352.      vs_clip(handle,0,NULL);
  353. }
  354.  
  355. void
  356. form_ncenter(tree,base)
  357. OBJECT *tree;
  358. GRECT *base;
  359. {
  360.     if(!rsc_fix)
  361.         init();
  362.  
  363.     if(ObW(ROOT) > base->g_w)
  364.         ObX(ROOT) = base->g_x-((ObW(ROOT)-base->g_w)/2);
  365.     else
  366.         ObX(ROOT) = base->g_x+((base->g_w-ObW(ROOT))/2);
  367.     if(ObH(ROOT) > base->g_h)
  368.         ObY(ROOT) = base->g_y-((ObH(ROOT)-base->g_h)/2);
  369.     else
  370.         ObY(ROOT) = base->g_y+((base->g_h-ObH(ROOT))/2);
  371.  
  372.     if(ObX(ROOT)<5)        /* Allow for Outline */
  373.         ObX(ROOT) = 5;
  374.     if(ObY(ROOT)<24)        /* Allow for Menu Bar */
  375.         ObY(ROOT) = 24;
  376.     if(ObX(ROOT)+ObW(ROOT)+5 > scrwid)
  377.         ObX(ROOT) = scrwid-ObW(ROOT);
  378.     if(ObY(ROOT)+ObH(ROOT)+5 > scrhigh)
  379.         ObY(ROOT) = scrhigh-ObH(ROOT);
  380.         
  381.     ObjcExtent(tree,base);
  382. }
  383.  
  384. /* Code below modified from Atari's Ken B's AESUTIL.C */
  385.  
  386. static void
  387. ObjcExtent( tree, r )
  388. OBJECT *tree;
  389. GRECT *r;
  390. {
  391.         short offset, border;
  392.         void *pspec;
  393.  
  394.         objc_offset( tree, ROOT, &r->g_x, &r->g_y );
  395.         *(long *)&r->g_w = *(long *)&ObW(ROOT);
  396.     
  397.         pspec = (void *)&ObSpec(ROOT);
  398.         border = ((ObInfo *)pspec)->border;
  399.         if(border>=0)
  400.         offset = 0;
  401.     else
  402.         offset = border, border = -border;
  403.     
  404.  
  405.         if( IsOutlined(ROOT) && offset > -3 )
  406.         offset = -3;
  407.  
  408.     if( offset ) {
  409.             r->g_x += offset;
  410.             r->g_w -= offset*2;
  411.             r->g_y += offset;
  412.             r->g_h -= offset*2;
  413.         }
  414.  
  415.     if( IsShadowed(ROOT) && border )
  416.     r->g_w += 2 * border, r->g_h += 2 * border;
  417. }    
  418.  
  419. void
  420. init()
  421. {
  422.     int dum,work_out[57];
  423.  
  424.     handle = graf_handle(&cwidth,&cheight,&dum,&dum);
  425.     vq_extnd(handle,0,work_out);
  426.     scrwid = work_out[0];
  427.     scrhigh = work_out[1];
  428.     vq_extnd(handle,1,work_out);
  429.     planes = work_out[4];    
  430.     
  431.     tree = (OBJECT *)&rs_object[0];
  432.     for(dum=0;dum<ANUM_OBS;dum++)
  433.         rsrc_obfix(tree,dum);
  434.     
  435.     rsc_fix = TRUE;
  436. }